using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SysFrame.Message;
using System.Reflection;
using System.Threading;

namespace SysFrame.BP
{
    /// <summary>
    /// 操作日志方法
    /// </summary>
    public class BLLsystem_16
    {
        public Isystem_16 interface_system_16;

        public BLLsystem_16()
        {
            object obj = BPBase.GetAchieve("SysFrame.BP", "system_16");
            interface_system_16 = (Isystem_16)obj;
        }


        /// <summary>
        /// 得到system_16实体
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public List<Mssystem_16> Getsystem_16MsByQuery(MsQuery msq)
        {
            return interface_system_16.Getsystem_16MsByQuery(msq);
        }

        /// <summary>
        /// 通过System_16_10得到实体
        /// </summary>
        /// <param name="System_16_10">ID</param>
        /// <returns>实体</returns>
        public Mssystem_16 Getsystem_16MsByKey(string System_16_10)
        {
            MsQuery msquery = new MsQuery();
            msquery.strquery += " and System_16_10='" + System_16_10 + "' ";
            List<Mssystem_16> lstData = Getsystem_16MsByQuery(msquery);
            return lstData[0];
        }

        /// <summary>
        ///  保存system_16信息
        /// </summary>
        /// <param name="ms">实体信息</param>
        /// <returns>消息</returns>
        public MsReturned Savesystem_16(Mssystem_16 ms)
        {
            return interface_system_16.Savesystem_16(ms);
        }

        /// <summary>
        /// 得到数据类型
        /// </summary>
        /// <param name="msquery">条件</param>
        /// <returns>数据类型</returns>
        public MsReturned Getsystem_16ByQuery(MsQuery msquery)
        {
            return interface_system_16.Getsystem_16ByQuery(msquery);
        }

        /// <summary>
        /// 删除system_16
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deletesystem_16ByQuery(MsQuery msq)
        {
            return interface_system_16.Deletesystem_16ByQuery(msq);
        }
        /// <summary>
        /// 删除system_16
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deletesystem_16ByKey(string System_16_10)
        {
            MsQuery msquery = new MsQuery();

            if (System_16_10.IndexOf(",") > 0)
            {
                msquery.strquery += " and System_16_10 in (" + System_16_10 + ") ";
            }
            else
            {
                msquery.strquery += " and System_16_10='" + System_16_10 + "' ";
            }
            return interface_system_16.Deletesystem_16ByQuery(msquery);
        }

        /// <summary>
        /// 登陆日志
        /// </summary>
        /// <param name="userid">登陆用户</param>
        /// <param name="b">登陆是否成功</param>
        /// <param name="msg">登陆备注</param>
        public static void LoginLog(string userid, bool b, string msg)
        {
            try
            {
                Mssystem_1 ms1 = new BLLsystem_1().Getsystem_1MsByKey(userid);
                ms1.sys10 = new BLLsystem_1().GetSys10ByUserID(ms1.System_1_10);
                Mssystem_16 ms = new Mssystem_16();
                ms.System_16_20 = ms1.System_1_20;//登录名
                ms.System_16_30 = ms1.System_1_120;//姓名
                ms.System_16_40 = "009001";//登陆日志
                ms.System_16_70 = "登陆";//登陆日志
                ms.System_16_80 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ms.System_16_90 = b ? "登陆成功" : "登陆失败";
                ms.System_16_90 += msg;//日志描述
                ms.System_16_100 = ms1.sys10.System_10_20 == null ? "" : ms1.sys10.System_10_20;//角色
                ms.System_16_110 = ms1.sys2 == null ? "" : ms1.sys2.System_2_30;//部门
                ms.System_16_120 = GetHostAddress();//IP地址
                ms.System_16_130 = GetMac();//MAC地址
                BLLsystem_16 bll = new BLLsystem_16();
                bll.Savesystem_16(ms);
            }
            catch { }
        }

        /// <summary>
        /// 用户点击菜单日志
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <param name="menuid">菜单ID</param>
        public static void ClickMenuLog(string userid, string menuid)
        {
            try
            {
                Mssystem_1 ms1 = new BLLsystem_1().Getsystem_1MsByKey(userid);
                Mssystem_7 ms7 = new BLLsystem_7().Getsystem_7MsByKey(menuid);
                ms1.sys10 = new BLLsystem_1().GetSys10ByUserID(ms1.System_1_10);
                Mssystem_16 ms = new Mssystem_16();
                ms.System_16_20 = ms1.System_1_20;//登录名
                ms.System_16_30 = ms1.System_1_120;//姓名
                ms.System_16_40 = "009002";//点击菜单日志
                ms.System_16_60 = ms7.strurl;//页面地址
                ms.System_16_70 = "打开页面";//登陆日志
                ms.System_16_80 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ms.System_16_90 = "打开页面[" + ms7.System_7_20 + "]";
                ms.System_16_100 = ms1.sys10.System_10_20 == null ? "" : ms1.sys10.System_10_20;//角色
                ms.System_16_110 = ms1.sys2 == null ? "" : ms1.sys2.System_2_30;//部门
                ms.System_16_120 = GetHostAddress();//IP地址
                ms.System_16_130 = GetMac();//MAC地址
                BLLsystem_16 bll = new BLLsystem_16();
                bll.Savesystem_16(ms);
            }
            catch
            { }
        }
        /// <summary>
        /// 记录顶级按钮日志
        /// </summary>
        /// <param name="userid">用户</param>
        /// <param name="menuid">菜单ID</param>
        /// <param name="streve">事件名称</param>
        public static void ButtonLog(string userid, string menuid, string streve)
        {
            try
            {
                //过滤_Click
                streve = streve.Replace("_Click", "");
                Mssystem_1 ms1 = new BLLsystem_1().Getsystem_1MsByKey(userid);
                Mssystem_7 ms7 = new BLLsystem_7().Getsystem_7MsByKey(menuid);
                ms1.sys10 = new BLLsystem_1().GetSys10ByUserID(ms1.System_1_10);
                List<string> btnlist = BLLsystem_1.GetAllBtnByMenu(menuid);
                if (btnlist.Contains(streve))
                {
                    Mssystem_8 ms8 = new BLLsystem_8().Getsystem_8MsByBtnID(streve);
                    Mssystem_16 ms = new Mssystem_16();
                    ms.System_16_20 = ms1.System_1_20;//登录名
                    ms.System_16_30 = ms1.System_1_120;//姓名
                    ms.System_16_40 = "009003";//点击按钮日志
                    ms.System_16_50 = ms7.System_7_20;//页面地址
                    ms.System_16_60 = ms7.strurl;//页面地址
                    ms.System_16_70 = "点击按钮";//登陆日志
                    ms.System_16_80 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    ms.System_16_90 = "点击按钮[" + ms8.System_8_20 + "]";
                    ms.System_16_100 = ms1.sys10.System_10_20 == null ? "" : ms1.sys10.System_10_20;//角色
                    ms.System_16_110 = ms1.sys2 == null ? "" : ms1.sys2.System_2_30;//部门
                    ms.System_16_120 = GetHostAddress();//IP地址
                    ms.System_16_130 = GetMac();//MAC地址
                    BLLsystem_16 bll = new BLLsystem_16();
                    bll.Savesystem_16(ms);
                }
            }
            catch
            {
            }
        }

        /// <summary>  
        /// 获取主机MAC地址  
        /// </summary>  
        public static string GetMac()
        {
            string Mac = "";
            return Mac;
        }

        /// <summary>
        /// 获取客户端IP地址（无视代理）
        /// </summary>
        /// <returns>若失败则返回回送地址</returns>
        public static string GetHostAddress()
        {
            return "127.0.0.1";
        }


        /// <summary>
        /// 检查IP地址格式
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
    }
}
